using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using System.ComponentModel;
using System.Windows.Forms;

namespace EntLibContrib.PolicyInjection.CallHandlers
{
    /// <summary>
    /// An attribute used to apply the <see cref="OneWayCallHandler"/> to the target.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Method)]
    public class ThreadSafeCallHandlerAttribute : HandlerAttribute
    {
        private string syncRoot;

        /// <summary>
        /// Creates a new <see cref="ThreadSafeCallHandlerAttribute"/>.
        /// </summary>
        public ThreadSafeCallHandlerAttribute()
        {
        }

        /// <summary>
        /// Creates a new <see cref="ThreadSafeCallHandlerAttribute"/> that uses the given
        /// syncRoot name.
        /// </summary>
        /// <param name="syncRoot">SyncRoot name to use for locking.</param>
        public ThreadSafeCallHandlerAttribute(string syncRoot)
        {
            SyncRoot = syncRoot;
        }

        /// <summary>
        /// Get or sets the SyncRootName used by the handler.
        /// </summary>
        /// <value>ThreadSafe name.</value>
        public string SyncRoot
        {
            get { return syncRoot; }
            set { syncRoot = value; }
        }

        /// <summary>
        /// Derived classes implement this method. When called, it
        /// creates a new call handler as specified in the attribute
        /// configuration.
        /// </summary>
        /// <returns>A new call handler object.</returns>
        public override ICallHandler CreateHandler()
        {
            return new ThreadSafeCallHandler(SyncRoot);
        }
    }
}
